package com.xyw.code.controller;


import com.xyw.code.dto.DictDTO;
import com.xyw.code.entity.SysDict;
import com.xyw.code.log.SysLog;
import com.xyw.code.service.ISysDictService;
import com.xyw.code.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author xuyiwei
 * @since 2019-07-30
 */
@RestController
@RequestMapping("/dict")
public class SysDictController {

    @Autowired
    private ISysDictService dictService;

    /**
     * 根据名称获取字典值详情
     * @param name
     * @return
     */
    @RequestMapping("/getDictDetailList")
    public Result selectDictDetailList(@RequestParam String name) {
        return Result.ok(dictService.selectDictDetailList(name));
    }


    /**
     * 获取字典列表集合
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:dict:view')")
    public Result getList(Integer page, Integer pageSize,String detailName,String value,String type) {
        return Result.ok(dictService.selectDictList(page, pageSize,detailName,value,type));
    }

    /**
     * 保存字典信息
     *
     * @param sysDict
     * @return
     */
    @SysLog(descrption = "新增字典")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:dict:add')")
    public Result add(@RequestBody SysDict sysDict) {
        return Result.ok(dictService.save(sysDict));
    }

    /**
     * 更新字典
     *
     * @param dictDto
     * @return
     */
    @SysLog(descrption = "更新字典")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:dict:update')")
    public Result update(@RequestBody DictDTO dictDto) {
        return Result.ok(dictService.updateDict(dictDto));
    }

    /**
     * 根据id删除字典
     * @param id
     * @return
     */
    @SysLog(descrption = "删除字典")
    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:dict:delete')")
    public Result delete(@PathVariable("id") int id) {
        return Result.ok(dictService.deleteDict(id));
    }

}

